package cn.stylefeng.guns.modular.stage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.stage.entity.Stage;
import cn.stylefeng.guns.modular.stage.model.params.StageParam;
import cn.stylefeng.guns.modular.stage.service.StageService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.nio.file.Path;
import java.nio.file.Paths;


/**
 * 阶段控制器
 *
 * @author CHENCHEN
 * @Date 2022-07-21 19:05:52
 */
@Slf4j
@Controller
@RequestMapping("/stage")
public class StageController extends BaseController {

    private String PREFIX = "/stage";

    @Autowired
    private StageService stageService;

    @Value("${video.file-path}")
    private String videoFolder;

    // 获取系统临时目录
//    private Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
    private Path tempDir = Paths.get("F:\\videoTemp");

    /**
     * 跳转到主页面
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/stage.html";
    }

    /**
     * 新增页面
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/stage_add.html";
    }

    /**
     * 编辑页面
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/stage_edit.html";
    }

    /**
     * 新增接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StageParam stageParam) {
        stageParam.setChapters(HtmlUtils.htmlUnescape(stageParam.getChapters().replace(" ","")));
        this.stageService.add(stageParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StageParam stageParam) {
        this.stageService.update(stageParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StageParam stageParam) {
        this.stageService.delete(stageParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StageParam stageParam) {
        Stage detail = this.stageService.getById(stageParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StageParam stageParam) {
        return this.stageService.findPageBySpec(stageParam);
    }

    /**
     * 根据专业ID获取下属阶段
     * @param specialtyId
     * @return
     */
    @ResponseBody
    @RequestMapping("/stageList")
    public ResponseData stageList(Long specialtyId) {
        return ResponseData.success(this.stageService.stageList(specialtyId));
    }

}


